<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="用户ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            type="nubmer"
            v-decorator="['user_id', { rules: [{ required: true,message: '请输入用户ID' }] }]"
            placeholder="请输入用户ID"
          />
        </a-form-item>
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['name']"
            placeholder="请输入名称"
          />
        </a-form-item>
        <a-form-item
          label="头像"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra="建议尺寸：96*96像素"
        >
          <SelectImage
            v-decorator="['image_id']"
          />
        </a-form-item>
<!--        <a-form-item label="毕业院校" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <a-input-->
<!--            v-decorator="['graduated']"-->
<!--            placeholder="请输入教练的毕业院校"-->
<!--          />-->
<!--        </a-form-item>-->
<!--        <a-form-item label="专业特点" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <a-input-->
<!--            v-decorator="['specialty']"-->
<!--            placeholder="请输入教练的专业特点"-->
<!--          />-->
<!--        </a-form-item>-->
<!--        <a-form-item label="教练简介" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <a-textarea-->
<!--            v-decorator="['brief']"-->
<!--            placeholder="请输入教练简介"-->
<!--          />-->
<!--        </a-form-item>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/course/coach'
import { SelectImage } from '@/components'

export default {
  props: {},
  components: {
    SelectImage
  },
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    add() {
      this.title = '添加工作人员'
      this.visible = true
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      const {
        form: { validateFields },
      } = this
      // 表单验证
      validateFields((errors, values) => {
        console.log(values)
        !errors && this.onFormSubmit(values)
      })
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.add(values)
        .then((result) => {
           // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },
  },
}
</script>
